<?php
/**
 * Category
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/7
 * Time: 15:55
 */

namespace app\common\model\mysql;


use think\Model;

class Category extends BaseModel
{

    public function getNormalCategorys($field = '*'){
        $where = [
            'status' => config('status.mysql.table_normal')
        ];
        $order = [
            'listorder'  => 'desc',
            'id' => 'desc',
        ];
        $result = $this->field($field)
                       ->where($where)
                       ->order($order)
                       ->select();
        return $result;
    }

    public function getLists($where,$num = 10){
        $order = [
            'listorder'  => 'desc',
            'id' => 'desc',
        ];
        $result = $this->where('status','<>',config('status.mysql.table_delete'))
                        ->where($where)
                        ->order($order)
                        ->paginate($num);
        return $result;
    }

    /**
     * 查询 pid 子分类数量
     * @param $condition
     * @return mixed
     */
    public function getChildCountInPids($condition){
        $where[] = ['pid' ,'in',$condition['pid']];
        $where[] = ['status' ,'<>',config('status.mysql.table_delete')];
        $res = $this->where($where)
                    ->field('pid,count(*) as count')
                    ->group('pid')
                    ->select();
        return $res;
    }
    public function getNormalByPid($pid = 0, $field = 'id,name,pid'){
        $res = $this->field($field)
                    ->where('pid',$pid)
                    ->where('status',config('status.mysql.table_normal'))
                    ->select();
        return $res;
    }

    /**
     * 通过多个id批量获正常取父级分类信息
     * @param $ids
     * @param bool $field
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalCategorysInPidsById($ids, $field = true, $in = 'id'){
        $where = [
            'status' => config('status.mysql.table_normal')
        ];
        $order = [
            'listorder'  => 'desc',
            'id' => 'desc',
        ];
        $result = $this->field($field)
            ->where($where)
            ->whereIn($in,$ids)
            ->order($order)
            ->select();
        return $result;
    }
}

